//package org.example.domain.strategy.service.rule.filter.impl;
//
//import lombok.extern.slf4j.Slf4j;
//import org.example.domain.strategy.model.entity.RuleActionEntity;
//import org.example.domain.strategy.model.entity.RuleMatterEntity;
//import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
//import org.example.domain.strategy.repository.IStrategyRepository;
//import org.example.domain.strategy.service.annotation.LoginStrategy;
//import org.example.domain.strategy.service.rule.filter.ILogicFilter;
//import org.example.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
//import org.example.types.common.Constants;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.*;
//
///**
// * 过滤权重规则
// */
//@Slf4j
//@Component
//@LoginStrategy(logicModel = DefaultLogicFactory.LogicModel.RULE_WIGHT)
//public class RuleWeightLogicFilter implements ILogicFilter<RuleActionEntity.RaffleBeforeEntity> {
//
//    @Resource
//    private IStrategyRepository strategyRepository;
//
//    private Long userScore = 4500L;
//
//    /**
//     * 权重规则过滤：
//     * 1.权重规则格式：4000:102,103,104,105 5000:102,103,104,105,106,107
//     * 2.解析数据格式：判断哪个范围符合用户的特定抽奖范围，比如有4500积分的用户的抽奖范围有哪些
//     * @param ruleMatterEntity
//     * @return
//     */
//    @Override
//    public RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> filter(RuleMatterEntity ruleMatterEntity) {
//        log.info("规则过滤-权重范围 userId:{} strategyId:{} ruleModel:{}", ruleMatterEntity.getUserId(), ruleMatterEntity.getStrategyId(), ruleMatterEntity.getRuleModel());
//
//        String userId = ruleMatterEntity.getUserId();
//        Long strategyId = ruleMatterEntity.getStrategyId();
//        String ruleValue = strategyRepository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId(), ruleMatterEntity.getRuleModel());
//
//        // 1. 根据用户ID查询用户抽奖消耗的积分值，本章节我们先写死为固定的值。后续需要从数据库中查询。
//        Map<Long, String> analyticalValueGroup = getAnalyticalValue(ruleValue);
//        if (null == analyticalValueGroup || analyticalValueGroup.isEmpty()) {
//            return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
//                    .code(RuleLogicCheckTypeVO.ALLOW.getCode())
//                    .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
//                    .build();
//        }
//
//        // 2. 转换Keys值，并默认排序
//        List<Long> analyticalSortedKeys = new ArrayList<>(analyticalValueGroup.keySet());
//        Collections.sort(analyticalSortedKeys);
//
//        // 3. 找出最小符合的值，也就是【4500 积分，能找到 4000:102,103,104,105】、【5000 积分，能找到 5000:102,103,104,105,106,107】
//        Long nextValue = analyticalSortedKeys.stream()
//                .filter(key -> userScore >= key)
//                .findFirst()
//                .orElse(null);
//
//        if (null != nextValue) {
//            return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
//                    .data(RuleActionEntity.RaffleBeforeEntity.builder()
//                            .strategyId(strategyId)
//                            .ruleWeightValueKey(analyticalValueGroup.get(nextValue))
//                            .build())
//                    .ruleModel(DefaultLogicFactory.LogicModel.RULE_WIGHT.getCode())
//                    .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
//                    .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
//                    .build();
//        }
//
//        return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
//                .code(RuleLogicCheckTypeVO.ALLOW.getCode())
//                .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
//                .build();
//    }
//
//    /**
//     * 解析权重规则配置字符串
//     * @param ruleValue
//     * @return
//     */
//    private Map<Long, String> getAnalyticalValue(String ruleValue) {
//        //根据空格拆分["4000:102,103", "5000:102,103,106"]
//        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);
//        Map<Long, String> ruleValueMap = new HashMap<>();
//        for (String ruleValueKey : ruleValueGroups) {
//            // 检查输入是否为空
//            if (ruleValueKey == null || ruleValueKey.isEmpty()) {
//                return ruleValueMap;
//            }
//            // 分割字符串以获取键和值
//            //冒号分割 parts = ["4000", "102,103"]
//            String[] parts = ruleValueKey.split(Constants.COLON);
//            //必须只有两部分，不然说明配置项出错
//            if (parts.length != 2) {
//                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueKey);
//            }
//            ruleValueMap.put(Long.parseLong(parts[0]), ruleValueKey);
//        }
//        //{4000L: "4000:102,103", 5000L: "5000:102,103,106"}
//        return ruleValueMap;
//    }
//}
